package health.lm.com.mine.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.lm.sjy.titlebar.widget.CommonTitleBar;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.entity.CashAccountEntity;
import health.lm.com.mine.entity.PutForwardBean;
import health.lm.com.mine.present.PutForwardPresenter;
import health.lm.com.mine.widget.CursorEditText;
import health.lm.com.router.ARouterConstant;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON;

/**
 * 提现
 */
@Route(path = ARouterConstant.PutForwardActivity, extras = ARouterConstant.NEED_LOGIN)
public class PutForwardActivity extends XActivity<PutForwardPresenter> {
    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @BindView(R2.id.tv_money)
    TextView tvBank;
    @BindView(R2.id.et_take_money)
    CursorEditText etTakeMoney;
    @BindView(R2.id.tv_take_all)
    TextView tvTakeAll;
    @BindView(R2.id.tv_put_forward)
    TextView tvPutForward;
    @BindView(R2.id.ll_layout)
    LinearLayout llLayout;
    @BindView(R2.id.tv_money_title)
    TextView tvMoneyTitle;
    @BindView(R2.id.tv_money_label)
    TextView tvMoneyLabel;
    @BindView(R2.id.tv_can_carry)
    TextView tvCanCarry;
    @BindView(R2.id.rb_ali)
    RadioButton rbAli;
    @BindView(R2.id.rb_card)
    RadioButton rbCard;
    @BindView(R2.id.rg_all)
    RadioGroup rgAll;
    @BindView(R2.id.et_name)
    EditText etName;
    @BindView(R2.id.et_account)
    EditText etAccount;
    private PutForwardBean bean;
    private CashAccountEntity entity;
    @Autowired
    String money;
    private String type = "2";

    @Override
    public int getLayoutId() {
        return R.layout.activity_put_forward;
    }

    @Override
    public PutForwardPresenter newP() {
        return new PutForwardPresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData(Bundle savedInstanceState) {
        //titlebar 返回按钮
        titlebar.setListener((v, action, extra) -> {
            if (action == ACTION_LEFT_BUTTON) {
                finish();
            }
        });
        rgAll.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                getType(i);
            }
        });
        rbAli.setChecked(true);
        //验证金额和账户
        Observable<CharSequence> moneyObservale = RxTextView.textChanges(etTakeMoney).debounce(500, TimeUnit.MILLISECONDS);
        Observable<CharSequence> cardObservale = RxTextView.textChanges(tvBank);
        Observable.combineLatest(moneyObservale, cardObservale, (money, card) -> {
            boolean isCard = card.toString().length() > 0;
            boolean isMoney = money.toString().length() > 0;

            return isCard && isMoney;
        }).compose(bindToLife())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aBoolean -> RxView.enabled(tvPutForward).accept(aBoolean));
//        //银行卡
//        RxView.clicks(tvBank)
//                .throttleFirst(2, TimeUnit.SECONDS)
//                .compose(bindToLife())
//                .subscribe(o -> {
//                    withValueActivity(ARouterConstant.CashAccountActivity).navigation(context, ARouterConstant.CARD_SELECT);
//                });
        //全部提现
        RxView.clicks(tvTakeAll)
                .throttleFirst(2, TimeUnit.SECONDS)
                .compose(bindToLife())
                .subscribe(o -> {
                    etTakeMoney.setText(money);
                });
        //提现
        RxView.clicks(tvPutForward)
                .throttleFirst(2, TimeUnit.SECONDS)
                .compose(bindToLife())
                .subscribe(o -> {
                    getP().putForward(type,
                            etAccount.getText().toString(),
                            etName.getText().toString(),
                            etTakeMoney.getText().toString().trim()
                            );
                });
        bean = new PutForwardBean();
        notifyView();
    }


    private void notifyView() {
        //可提现金额
        tvBank.setText(money);
        //可提现金额
        tvCanCarry.setText(String.format("可提现金额￥%s", money));
    }

    private void getType(int id){
        if (id == R2.id.rb_ali){
            type = "2";
        }else if (id == R2.id.rb_card){
            type = "3";
        }
    }
    public void puforwardSuccess(){
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            if (null == bundle) {
                return;
            }
            if (ARouterConstant.CARD_SELECT == requestCode) {
                entity = (CashAccountEntity) bundle.getSerializable("entity");
                if (entity != null) {
                    tvBank.setText(entity.getTitle());
                }
            }
        }
    }

}
